package com.gemme.gemme.view;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gemme.gemme.R;
import com.gemme.gemme.home.Home2Fragment;
import com.gemme.gemme.home.PageAllProductBean;
import com.gemme.gemme.wode.ProductNewActivity;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

public class RefreshView extends RelativeLayout {


    private Context context;
    private GMOnFreshListener gmOnFreshListener;

    public RefreshView(Context context) {
        this(context,null);
    }

    public RefreshView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RefreshView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }
    private SwipeRefreshLayout mRefreshLayout;
    private SwipeRecyclerView mRecyclerView;
    private void initView() {
        LayoutInflater.from(context).inflate(R.layout.view_refresh,this);
        mRefreshLayout = findViewById(R.id.refresh_layout);
        mRecyclerView = findViewById(R.id.recycler_view);


        // 自定义的核心就是DefineLoadMoreView类。
        DefineLoadMoreView loadMoreView = new DefineLoadMoreView(context);
        mRecyclerView.addFooterView(loadMoreView); // 添加为Footer。
        mRecyclerView.setLoadMoreView(loadMoreView); // 设置LoadMoreView更新监听。
        mRefreshLayout.setOnRefreshListener(mRefreshListener); // 去除下拉刷新监听。
        mRecyclerView.setLoadMoreListener(mLoadMoreListener); // 加载更多的监听。
//        mRecyclerView.loadMoreFinish(false, true);
        //todo 暂时去掉加载更多
        mRecyclerView.loadMoreFinish(false, false);


        mRefreshLayout.setEnabled(false);
       /* RefreshAdapter homeNewAdapter = new RefreshAdapter(null);
        mRecyclerView.setAdapter(homeNewAdapter);*/
    }


      /* //布局管理器
       new LinearLayoutManager(this);

        new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);


        mRecyclerView.setLayoutManager(layoutManager);


        */

      public void canRefresh(boolean iscan){
          mRefreshLayout.setEnabled(iscan);
      }

    //设置布局管理器
    public void setLayoutManager (RecyclerView.LayoutManager layoutManager){
        mRecyclerView.setLayoutManager(layoutManager);
    }

    public void addItemDecoration(RecyclerView.ItemDecoration decor){
        mRecyclerView.addItemDecoration(decor);
    }

    public void addHeaderView(GuanzhuHeadView guanzhuHeadView) {
        mRecyclerView.addHeaderView(guanzhuHeadView);
    }

    //设置适配器
    public void setAdapter(RecyclerView.Adapter adapter){
        mRecyclerView.setAdapter(adapter);
    }

    /**
     * 加载更多。
     */
    private SwipeRecyclerView.LoadMoreListener mLoadMoreListener = new SwipeRecyclerView.LoadMoreListener() {
        @Override
        public void onLoadMore() {
            System.out.println("");
            if(gmOnFreshListener != null)
                gmOnFreshListener.onLoadMore();
/*
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
//                    mRecyclerView.loadMoreFinish(false, false);
                }
            }, 1000);
*/

        }
    };

    /**
     * 下拉刷新。
     */
    private SwipeRefreshLayout.OnRefreshListener mRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            System.out.println();
            //一秒后关闭下拉刷新
            if(gmOnFreshListener != null)
                gmOnFreshListener.onRefresh();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mRefreshLayout.setRefreshing(false);
                }
            }, 1000);

           /* mRecyclerView.postDelayed(new Runnable() {
                @Override
                public void run() {
//                    loadData();
                    initData();
                }
            }, 1000); // 延时模拟请求服务器。*/
        }
    };

    public void setGMOnFreshListener(GMOnFreshListener gmOnFreshListener){

        this.gmOnFreshListener = gmOnFreshListener;
    }

    public void setGMOnFreshListener(boolean isCanFresh,GMOnFreshListener gmOnFreshListener){
        mRefreshLayout.setEnabled(isCanFresh);
        this.gmOnFreshListener = gmOnFreshListener;
    }


    public void loadMoreFinish(boolean dateEmpty, boolean hasmore) {
        mRecyclerView.loadMoreFinish(dateEmpty, hasmore);
    }


    public interface GMOnFreshListener{
        void onRefresh();
        void onLoadMore();
    }

    public void setRefreshing(boolean isRefreshing){
        mRefreshLayout.setRefreshing(isRefreshing);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if(isRefreshing)
                mRefreshLayout.setRefreshing(false);
            }
        }, 1000);
    }


    public static class RefreshBean{
        int pageIndex ;
        int pageSize;
        boolean isRefresh = true;  //是下拉刷新吗

        public boolean isRefresh() {
            return isRefresh;
        }

        public void setRefresh(boolean refresh) {
            isRefresh = refresh;
        }



        public int getPageIndex() {
            return pageIndex;
        }

        public void setPageIndex(int pageIndex) {
            this.pageIndex = pageIndex;
        }

        public int getPageSize() {
            return pageSize;
        }

        public void setPageSize(int pageSize) {
            this.pageSize = pageSize;
        }
    }



/*
    public class RefreshAdapter extends RecyclerView.Adapter<RefreshAdapter.ViewHolder>{
        private List<PageAllProductBean.DataBean.RecordsBean> mDataList;

        public RefreshAdapter( List<PageAllProductBean.DataBean.RecordsBean>  records){
            mDataList = records;
        }
        public void setData(List<PageAllProductBean.DataBean.RecordsBean> records) {
            mDataList = records;
            notifyDataSetChanged();
        }
        @NonNull
        @Override
        public RefreshAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.home_new_item, null);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            inflate.setLayoutParams(layoutParams);
            return new RefreshAdapter.ViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull RefreshAdapter.ViewHolder holder, int position) {
            try {
                PageAllProductBean.DataBean.RecordsBean recordsBean = mDataList.get(position);

                holder.name.setText(recordsBean.getProductName());
                holder.price.setText("¥"+recordsBean.getProductPrice() + "");
                Glide.with(holder.picture.getContext()).load(recordsBean.getAbbreviationPic()).into(holder.picture);

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(context, ProductNewActivity.class);
                        intent.putExtra("id",recordsBean.getId());
                        context.startActivity(intent);

                    }
                });
            }catch (Exception e){

            }
        }

        @Override
        public int getItemCount() {
            if(mDataList == null){
                return 10;
            }
            return mDataList.size();
        }



        public class ViewHolder extends RecyclerView.ViewHolder {
            TextView name;
            TextView price;
            ImageView picture;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                name = itemView.findViewById(R.id.name);
                price = itemView.findViewById(R.id.price);
                picture = itemView.findViewById(R.id.picture);
            }
        }
    }
*/


}
